//8.发布订阅模式
class Agent{
    constructor(){
        this._events = {};
    };
    //on addEventListener
    subscribe(type,listener){
        let listeners = this._events[type];
        if(listeners){
            listeners.push(listener);
        }else{
            this._events[type] = [listener];
        }
    }
    //emit
    publish(type){
        let listeners = this._events[type];
        let args = Array.prototype.slice.call(arguments,1);
        if(listeners){
            listeners.forEach(element => {
                element(...args);
            });
        }
    }
};
//房东
class LandLord{
    constructor(name){
        this.name = name;
    };
    //去中介平台注册出租
    lend(agent,area,price){
        agent.publish('house',area,price);
    }
}
//租客
class Tenant{
    constructor(name){
        this.name = name;
    };
    //租房
    rent(agent){
        agent.subscribe('house',(area,price)=>{
            console.log(this.name,'被通知有房源啦---',area,price);
        });
    }
}
let agent1 = new Agent();
let t1 = new Tenant("张三");
let t2 = new Tenant("李四");
t1.rent(agent1);
t2.rent(agent1);
let landLord1 = new LandLord('房东1');
landLord1.lend(agent1,'60平方',8000);